package com.sbtr.controller.feature;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sbtr.business.feature.dto.FeDatafieldDto;
import com.sbtr.business.feature.entity.FeDatafield;
import com.sbtr.business.feature.entity.FeDatafile;
import com.sbtr.business.feature.service.FeDatafieldService;
import com.sbtr.business.feature.service.FeDatafileService;
import com.sbtr.common.PageDto;
import com.sbtr.common.Response;
import com.sbtr.controller.common.CommonController;
import com.sbtr.exception.BDException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 原始数据文件属性字段列表 前端控制器
 *
 * @author dgl
 * @since 2023-03-17
 */
@RestController
@RequestMapping("/feDatafield")
@Api(tags = "原始数据文件属性字段列表")
public class FeDatafieldController extends CommonController<FeDatafield, FeDatafieldService> {

    @Autowired
    FeDatafileService feDatafileService;
    @Autowired
    FeDatafieldService feDatafieldService;

    @GetMapping("/pageList")
    @ApiOperation(httpMethod = "GET", value = "原始数据文件属性字段列表列表")
    public Response<PageDto<FeDatafield>> pageList(FeDatafieldDto feDatafieldDto) {
        return Response.success(u.pageList(feDatafieldDto));
    }

    @GetMapping("/getListByOperatorId")
    @ApiOperation(httpMethod = "GET", value = "根据算子id获取字段列表")
        public Response<List<FeDatafield>> getListByOperatorId(String operatorId) {
        QueryWrapper<FeDatafile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("processing_id",operatorId);
        queryWrapper.last(" limit 1");
        FeDatafile feDatafile=feDatafileService.getOne(queryWrapper);

        if(feDatafile==null){
            return Response.error(500,"请先上传文件");
        }

        QueryWrapper<FeDatafield> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("datafile_id",feDatafile.getId());
        return Response.success(u.list(queryWrapper1));
    }

    @PostMapping("/save")
    @ApiOperation(httpMethod = "POST", value = "原始数据文件属性字段列表保存")
    public Response<FeDatafield> save(@RequestBody FeDatafield feDatafield) {
        feDatafield.setId(null);
        if (feDatafield.insert()) {
            return Response.success(feDatafield);
        } else throw new BDException("保存失败");
    }

}

